package com.yufei.infoExtractor.util;

import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.BitSet;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.httpclient.URIException;
import org.apache.commons.httpclient.util.URIUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.yufei.infoExtractor.io.AbstractHttpDataRetriever;
import com.yufei.infoExtractor.io.HttpDataRetriever;
import com.yufei.infoExtractor.io.DataRetrieverFactory;
import com.yufei.infoExtractor.io.DataRetrieverFeatures;
import com.yufei.infoExtractor.pfw.entity.Relatedlink;

public class URLUtil {

    private static final Logger log = Logger.getLogger(URLUtil.class);

    private static final BitSet WWW_FORM_URL = new BitSet(256);

    static {
        // alpha characters
        for (int i = 'a'; i <= 'z'; i++) {
            WWW_FORM_URL.set(i);
        }
        for (int i = 'A'; i <= 'Z'; i++) {
            WWW_FORM_URL.set(i);
        }
        // numeric characters
        for (int i = '0'; i <= '9'; i++) {
            WWW_FORM_URL.set(i);
        }
        // blank to be replaced with +
        //WWW_FORM_URL.set(' ');
        WWW_FORM_URL.set('-');
        WWW_FORM_URL.set('_');
        WWW_FORM_URL.set(',');
        WWW_FORM_URL.set('.');
        WWW_FORM_URL.set(':');
        WWW_FORM_URL.set('*');
        WWW_FORM_URL.set('/');
        WWW_FORM_URL.set('&');
        WWW_FORM_URL.set('?');
        WWW_FORM_URL.set('%');
        WWW_FORM_URL.set('=');
        WWW_FORM_URL.set('#');
        WWW_FORM_URL.set('@');
    }
    
    

    private static URLUtil instance = new URLUtil();

    private URLUtil() {}

    public static URLUtil getInstance() {
        return instance;
    }

    /**
     * 对URL进行规范化，将汉字转码，并全部转换成小写
     * @param url 待规范化的URL
     * @return 规范化后的URL
     */
    public String regulateURL(String url) {
        
            try {
				url = URIUtil.encode(url, WWW_FORM_URL, "utf-8");
			} catch (URIException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
       
        if(!url.contains("://")) {
            url = "http://" + url;
        }
        return url;
    }

    public String regUrl(String url) {
        if(url != null && !"".equals(url)) {
            url = url.trim();
        }
       
            try {
				url = URIUtil.encode(url, WWW_FORM_URL, "utf-8");
			} catch (URIException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
      
        if(!url.contains("://")) {
            url = "http://" + url;
        }
        return url;
    }

    /**
     * 判断节目是否是基于HTTP协议的
     * @param url 待判别的URL
     * @return true : 是HTTP协议的 ，反之为false
     */
    public boolean isHttp(String url) {
        if(StringUtils.isNotEmpty(url)) {
            String lowerUrl = url.toLowerCase();
            return lowerUrl.startsWith("http://");
        } else {
            return false;
        }
    }

    public String getProtocol(String url) {

        if(url.indexOf("://") < 0) {
            url = "http://" + url;
        }
        return url.substring(0, url.indexOf("://"));
    }

    public URL normalUrl(String url) {
        String site = DomainUtil.getSite(url);
        try {
            String protocol = URLUtil.getInstance().getProtocol(url);
            if(protocol.equalsIgnoreCase("http")) {
                URI baseURI = new URI("http://" + site);
                URIResolver resolver = new URIResolver(baseURI);
                URI uri = resolver.resolve(url);
                String strResult =  URLUtil.getInstance().regUrl(uri.toString());
                try {
                    if(strResult.indexOf("://") > 0) {
                        return new URL(strResult);
                    }
                } catch (MalformedURLException e) {
                    log.error("URL格式不正确", e);
                }
            }
        } catch (URISyntaxException e) {
            log.error("URL格式不正确", e);
            String strResult =  URLUtil.getInstance().regUrl(url);
            try {
                if(strResult.indexOf("://") > 0) {
                    return new URL(strResult);
                }
            } catch (MalformedURLException e1) {
                log.error("URL格式不正确", e1);
            }
        } catch(Exception e) {
            log.error("URL包含未知错误", e);
        }
        return null;
    }

    public URL normalUrl(String baseUrl, String url) {
        String site = DomainUtil.getSite(url);
        try {
            String protocol = URLUtil.getInstance().getProtocol(url);
            if(protocol.equalsIgnoreCase("http")) {
                URI baseURI;
                if(StringUtils.isNotEmpty(DomainUtil.getTopDomain(url))) {
                    baseURI = new URI("http://" + site);
                } else {
                    if(baseUrl.startsWith("http://")) {
                        baseURI = new URI(baseUrl);
                    } else {
                        baseURI = new URI("http://" + baseUrl);
                    }
                }

                URIResolver resolver = new URIResolver(baseURI);
                URI uri = resolver.resolve(url);
                String strResult =  URLUtil.getInstance().regUrl(uri.toString());
                try {
                    if(strResult.indexOf("://") > 0) {
                        return new URL(strResult);
                    }
                } catch (MalformedURLException e) {
                    log.error("URL格式不正确", e);
                }
            }
        } catch (URISyntaxException e) {
            log.error("网站URL格式不正确", e);
            String strResult =  URLUtil.getInstance().regUrl(url);
            try {
                if(strResult.indexOf("://") > 0) {
                    return new URL(strResult);
                }
            } catch (MalformedURLException e1) {
                log.error("URL格式不正确", e1);
            }
        }
        return null;
    }
public static String getBaseUrlFromUrl(String url){
	if(!url.contains("?")){
		return url;
	}
	url=url.substring(0, url.lastIndexOf("?"));
	return url;
}
/**
 * 从url中解析出域名
 */
public static String getDomainFromUrl(String url){
	//网址类似于这种情况：http://www.iteye.com
	
	if(url.lastIndexOf("/")==6){
		url+="/";
	}
	String domain = null;
	String domainRegex="http://(.*?)/";
    Pattern pattern =Pattern.compile(domainRegex, Pattern.CASE_INSENSITIVE);
		Matcher matcher = pattern.matcher(url);
		String match=null;
		
		while(matcher.find()){
			domain=matcher.group(1);
		}
		
	return domain;
}
//将不完整的url处理成完整的URL
public static String formatUrlWithBaseUrl(String url,String baseUrl){
	url=baseUrl+url;
	
	url=filterUrl(url);
	return url;
}
//将不完整的url处理成完整的URL
public static String formatUrl(String url,String domain){
	//不需要进行完善的链接
	if(url.startsWith("http://")||url.startsWith("rtsp://")){
		return url;
	}
	if(url.indexOf("/",0) != 0){
		url="/"+url;
	}
	url="http://"+domain+url;
	
	url=filterUrl(url);
	return url;
}
//过滤url中的特殊字符比如：空格等
private static String filterUrl(String url){
	String[] filterdStrs={"amp;"," "};
	for(String str:filterdStrs){
		url=url.replace(str, "");
	}
	return url;
}
    public static void main(String[] args) {
//        URLUtil instance = URLUtil.getInstance();
//        System.err.println("http://vhead.blog.sina.com.cn/player/bnplayer.swf?vid=3358649&chn1=167&title=电影：《力王之真正敌人》(下)");
//        String regURL = instance.regUrl("http://vhead.blog.sina.com.cn/player/bnplayer.swf?vid=3358649&chn1=167&title=电影：《力王之真正敌人》(下)");
//        System.err.println(regURL);

//        URI baseUri = null;
//        try {
//            baseUri = new URI("sina.com.cn");
//        } catch (URISyntaxException e) {
//            e.printStackTrace();
//        }
//        URIResolver resolver = new URIResolver(baseUri);
//        URI uri = resolver.resolve("sina.com.cn");
//        String url = URLUtil.getInstance().regUrl(uri.toString());
//        System.out.println(url);

        //System.out.println(getProtocol("http://sina.com.cn"));
    	String url="http://www.iteye.com/sg/dg/dg/dg//dg/dg";
    	 System.out.println(getDomainFromUrl(url));
    	
    }


   public static String getIpFromDomain(String domainName){
	String ip=null;
	try {
		InetAddress ad=InetAddress.getByName(domainName);
		ip=ad.getHostAddress();
	} catch (UnknownHostException e) {
		// TODO Auto-generated catch block
		if(log.isDebugEnabled()){
			log.debug("请检查域名设置是否正确！");
		}
			
	}
	
	return null;
	   
   }
	/**
	 * @deprecated Use {@link CommonUtil#cleanHtml(String)} instead
	 */
	public static String cleanHtml(String about) {
		return CommonUtil.cleanHtml(about);
	}

}
